
package be.kuleuven.cs.mop.domain.model.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class FocusWorkAlgorithmTaskType extends FocusWorkAlgorithm {

	private final TaskTypeImpl type;
	
	/**
	 * Represents a tasktype-based template method implementation for the "Focus Work" use case
	 */
	public FocusWorkAlgorithmTaskType(UserImpl user,TaskTypeImpl type) {
		super(user);
		if(type == null){
			throw new NullPointerException("type == null");
		}
		this.type = type;
	}


	/**
	 * Compares two tasks based on their deadline
	 */
	protected Comparator<TaskImpl> getComparator() {
		return new Comparator<TaskImpl>() {
			@Override
			public int compare(final TaskImpl a, final TaskImpl b) {
				return a.getSchedule().getDeadline().compareTo(
						b.getSchedule().getDeadline());
			}
		};
	}

	/**
	 * Sorts the list of tasks based on the comparator above
	 */
	@Override
	protected void sort(final List<TaskImpl> tasks) {
		Collections.sort(tasks, getComparator());
	}

	/**
	 * Filters all tasks out that are not available or not have the wanted type
	 */
	@Override
	protected void filter(List<TaskImpl> tasks) {
		ArrayList<TaskImpl> tasksToBeRemoved = new ArrayList<TaskImpl>();
		for(TaskImpl task : tasks) {
			if (!task.isAvailable() || !task.getType().equals(type))
				tasksToBeRemoved.add(task);
		}
		tasks.removeAll(tasksToBeRemoved);
	}

}
